package com.newland.mesdk.demo.fragment;

import java.util.concurrent.TimeUnit;

import android.app.Fragment;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

import com.newland.me.SupportMSDAlgorithm;
import com.newland.mesdk.demo.main.R;
import com.newland.mtype.DeviceRTException;
import com.newland.mtype.ModuleType;
import com.newland.mtype.common.MESeriesConst;
import com.newland.mtype.event.DeviceEventListener;
import com.newland.mtype.module.common.cardreader.CommonCardType;
import com.newland.mtype.module.common.cardreader.OpenCardReaderEvent;
import com.newland.mtype.module.common.cardreader.OpenCardReaderResult;
import com.newland.mtype.module.common.pin.WorkingKey;
import com.newland.mtype.module.common.swiper.SwipResult;
import com.newland.mtype.module.common.swiper.SwipResultType;
import com.newland.mtype.module.common.swiper.SwiperReadModel;
import com.newland.mtype.util.Dump;
import com.newland.mesdk.demo.interfaceimpl.CardReaderInterfaceImpl;
import com.newland.mesdk.demo.interfaceimpl.SwiperInterfaceImpl;
import com.newland.mesdk.demo.main.MainActivity;
import com.newland.mesdk.demo.main.MyApplication;
import com.newland.mesdk.demo.util.AppExCode;
import com.newland.mesdk.demo.util.Const;
import com.newland.mesdk.demo.util.Const.MessageTag;

/**
 * Created by YJF 刷卡模块
 */
public class SwipFragment extends Fragment implements OnClickListener {
	private MainActivity mainActivity;
	private Button btnTrackByMask, btnTrack, btnPlainTrack;
	private SwiperInterfaceImpl swiperInterfaceImpl;
	private CardReaderInterfaceImpl cardReaderInterfaceImpl;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_swip, null);
		mainActivity = (MainActivity) getActivity();
		initSwipView(view);
		return view;
	}

	public void initSwipView(View view) {
		btnTrackByMask = (Button) view.findViewById(R.id.btn_swip_read_encryresult_by_mask);
		btnTrackByMask.setOnClickListener(this);

		btnTrack = (Button) view.findViewById(R.id.btn_swip_read_encryresult);
		btnTrack.setOnClickListener(this);

		btnPlainTrack = (Button) view.findViewById(R.id.btn_swip_plain_result);
		btnPlainTrack.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		if (mainActivity.isDeviceInit()) {
			if (mainActivity.processingisLocked()) {
				mainActivity.showMessage("请先完成或撤销当前操作...", MessageTag.ERROR);
			} else {
				mainActivity.connectDevice();
				cardReaderInterfaceImpl = new CardReaderInterfaceImpl();
				swiperInterfaceImpl = new SwiperInterfaceImpl();
				switch (v.getId()) {
				case R.id.btn_swip_read_encryresult_by_mask:
					new Thread(new Runnable() {

						@Override
						public void run() {
							mainActivity.processingLock();
							try {
								mainActivity.showMessage("开始读取掩码方式的加密的磁道信息" + "\r\n", MessageTag.NORMAL);
								SwipResult swipRslt = swiperInterfaceImpl
										.readEncryptResult(
												new SwiperReadModel[] { SwiperReadModel.READ_SECOND_TRACK,
														SwiperReadModel.READ_THIRD_TRACK },
												new WorkingKey(Const.DataEncryptWKIndexConst.DEFAULT_TRACK_WK_INDEX),
												new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 },
												SupportMSDAlgorithm
														.getMSDAlgorithm(MESeriesConst.TrackEncryptAlgorithm.BY_UNIONPAY_MODEL));
								if (null != swipRslt && swipRslt.getRsltType() == SwipResultType.SUCCESS) {
									((MyApplication) (mainActivity).getApplication()).setSwipResult(swipRslt);
									byte[] secondTrack = swipRslt.getSecondTrackData();
									byte[] thirdTrack = swipRslt.getThirdTrackData();
									mainActivity.showMessage(
											"二磁道:" + (secondTrack == null ? "null" : Dump.getHexDump(secondTrack))
													+ "\r\n", MessageTag.DATA);
									mainActivity.showMessage(
											"三磁道:" + (thirdTrack == null ? "null" : Dump.getHexDump(thirdTrack))
													+ "\r\n", MessageTag.DATA);
								} else {
									mainActivity.showMessage("刷卡结果  空了,请冲刷" + "\r\n", MessageTag.ERROR);
								}
								mainActivity.processingUnLock();
							} catch (Exception e) {
								mainActivity.showMessage("读取掩码方式的加密的磁道信息异常" + "\r\n", MessageTag.ERROR);
								mainActivity.showMessage(e.getMessage()  + "\r\n", MessageTag.ERROR);
								mainActivity.showMessage("是否已经加载主密钥、工作秘钥、AID、公钥！" + "\r\n", MessageTag.ERROR);
								mainActivity.showMessage("是否已经开启读卡器并刷卡！" + "\r\n", MessageTag.ERROR);
								mainActivity.processingUnLock();
							}

						}
					}).start();

					break;
				case R.id.btn_swip_read_encryresult:
					new Thread(new Runnable() {

						@Override
						public void run() {
							mainActivity.processingLock();
							try {
								mainActivity.showMessage("开始读取加密的磁道信息" + "\r\n", MessageTag.NORMAL);
								SwipResult swipRslt = swiperInterfaceImpl
										.readEncryptResult(
												new SwiperReadModel[] { SwiperReadModel.READ_SECOND_TRACK,
														SwiperReadModel.READ_THIRD_TRACK },
												new WorkingKey(Const.DataEncryptWKIndexConst.DEFAULT_TRACK_WK_INDEX),
												SupportMSDAlgorithm
														.getMSDAlgorithm(MESeriesConst.TrackEncryptAlgorithm.BY_UNIONPAY_MODEL));
								if (null != swipRslt && swipRslt.getRsltType() == SwipResultType.SUCCESS) {
									((MyApplication) (mainActivity).getApplication()).setSwipResult(swipRslt);
									byte[] secondTrack = swipRslt.getSecondTrackData();
									byte[] thirdTrack = swipRslt.getThirdTrackData();

									mainActivity.showMessage(
											"二磁道:" + (secondTrack == null ? "null" : Dump.getHexDump(secondTrack))
													+ "\r\n", MessageTag.DATA);
									mainActivity.showMessage(
											"三磁道:" + (thirdTrack == null ? "null" : Dump.getHexDump(thirdTrack))
													+ "\r\n", MessageTag.DATA);
								} else {
									mainActivity.showMessage("刷卡结果  空了" + "\r\n", MessageTag.ERROR);
								}
								mainActivity.processingUnLock();
							} catch (Exception e2) {
								mainActivity.showMessage("刷卡异常" + e2 + "\r\n", MessageTag.ERROR);
								mainActivity.showMessage("是否已经加载主密钥、工作秘钥、AID、公钥！" + "\r\n", MessageTag.ERROR);
								mainActivity.showMessage("是否已经开启读卡器并刷卡！" + "\r\n", MessageTag.ERROR);
								mainActivity.processingUnLock();
							}

						}
					}).start();

					break;
				case R.id.btn_swip_plain_result:
					new Thread(new Runnable() {

						@Override
						public void run() {
							mainActivity.processingLock();
							try {
								mainActivity.showMessage("开始明文方式返回刷卡结果:" + "\r\n", MessageTag.NORMAL);
								SwipResult swipRslt = swiperInterfaceImpl.readPlainResult(new SwiperReadModel[] {
										SwiperReadModel.READ_SECOND_TRACK, SwiperReadModel.READ_THIRD_TRACK });
								if (null != swipRslt && swipRslt.getRsltType() == SwipResultType.SUCCESS) {

									((MyApplication) (mainActivity).getApplication()).setSwipResult(swipRslt);
									byte[] secondTrack = swipRslt.getSecondTrackData();
									byte[] thirdTrack = swipRslt.getThirdTrackData();
									mainActivity.showMessage(
											"二磁道:" + (secondTrack == null ? "null" : Dump.getHexDump(secondTrack))
													+ "\r\n", MessageTag.DATA);
									mainActivity.showMessage(
											"三磁道:" + (thirdTrack == null ? "null" : Dump.getHexDump(thirdTrack))
													+ "\r\n", MessageTag.DATA);
								} else {
									mainActivity.showMessage("刷卡结果  空了" + "\r\n", MessageTag.ERROR);
								}
								mainActivity.processingUnLock();
							} catch (Exception e) {
								mainActivity.showMessage("获取磁道明文异常：" + e + "\r\n", MessageTag.ERROR);
								mainActivity.showMessage("是否已经加载主密钥、工作秘钥、AID、公钥！" + "\r\n", MessageTag.ERROR);
								mainActivity.showMessage("是否已经开启读卡器并刷卡！" + "\r\n", MessageTag.ERROR);
								mainActivity.processingUnLock();
							}
						}
					}).start();

					break;

				default:
					break;
				}
			}
		} else {
			mainActivity.showMessage("设备未初始化，请先初始化！" + "\r\n", MessageTag.TIP);
		}
	}

}
